import Vue from 'vue'
import Router from 'vue-router'
import web from './web'
import view from '../lib/view'
import store from '../store/index'

Vue.use(Router)

const router = new Router({
  mode: 'history',
  routes: web
})

//beforeEach是router的钩子函数，在进入路由前执行
router.beforeEach((to, from, next) => {
    view.startLoginbar();
    if (to.meta.title) {//判断是否有标题
        document.title = to.meta.title
    }
    //从localStorage中读取token
    if (localStorage.getItem('token')) {
        store.commit('SET_TOKEN', localStorage.getItem('token'))
    }
    if (to.path !== '/login' && !store.state.token.token) {
        view.showMsg("您当前未登陆！", "error",);
        next({
            path: '/login'
        })
    }
    next();
});
router.afterEach(() => {
    //路由结束 进度条停止
    view.finishLoginbar()
});

export default router